/*
 * WX_3.c
 *
 * Created: 2013-05-09 11:17:59
 *  Author: ant
 */ 
#include "wx-3.h"

#include "../sc-k/utils.h"

#include "../sc-k/analog.h"
#include "../sc-k/digital.h"
#include "../sc-k/trans-k.h"
//#include "avr/wdt.h"

#include "../sc-k/SWLcd.h"

#include <avr/sleep.h>

u8 ksp = 3;		
u8 uw_alarms;


static u8 maximum = MAX_BITS_LONG;

u8 xxxtime = 0;

#define EXIT_INT1_VECTOR { EIFR |= ( 1 << INTF1) ;  TIFR1 |= (1 << OCF1A); T1_ENABLE; return;  }

u16 backup_value = 0;

u8 sleep_flag = 0;

ISR(INT1_vect) // alarm / trans
{
	sleep_disable();
	sleep_flag = 0;
	
	T1_DISABLE;
	logon = 0;

	u16 val = TCNT1;
	TCNT1 = 0;
	T1START;
	
	if (val > GF0)
	{
		//xxx = bits;
		bits = 0;
		polarity = 0;
		T1STOP;
		TCNT1 = 0;
		EXIT_INT1_VECTOR;
	}
		
	if (!polarity)
	{
		LY_OFF;
		polarity = 1;
		EXIT_INT1_VECTOR;
	}
	else
	{
		trans_f[bits] = val;
		if (bits > 65)
			bits = 0;
		bits++;
		polarity = 0;
		LY_ON;
		LY_OFF;
		EXIT_INT1_VECTOR;
	
	}

}

u8 dbg_timeout = 0;


u8 timer_no_comm = 0;

//6s /(255us*1024presc*10^-6) = 23
ISR(TIMER2_OVF_vect)
{
	timer_no_comm ++;
	if (timer_no_comm > 23)
	{
		timer_no_comm = 23;
		logged = 0;
	}
}

ISR(TIMER1_COMPA_vect)		// timeout of 
{
	//PORTB |= (1 << 7);
	u8 max = 0;
	
	if (bits >= MAX_BITS_SHORT)
	{
		if (identify(trans_f[1]))
		{
			//PORTB &= ~(1 << 7);
			max = MAX_BITS_SHORT;		// 1
			//PORTB |= (1 << 7);
		}
		else
		{
			max = MAX_BITS_LONG;		// 0
		}
	}
	else
	{
		T1STOP;
		TCNT1 = 0;
		bits = 0;
		polarity = 0;
		INT1_ENABLE;
		sleep_flag = 0;
	}
	

	if (bits < max || !max)
	{
		T1STOP;
		TCNT1 = 0;
		bits = 0;
		polarity = 0;
		
		T2_STOP;

		INT1_ENABLE;
		
	}

	for (u8 c = 0; c < max; c++)
	{
		rbuff[c] = identify(trans_f[c]);
	}
	dig_trans_finish = max;
	sleep_flag = 0;
	//PORTB &= ~(1 << 7);
	
}

ISR(TIMER1_OVF_vect)
{
	
}

int main(void)
{
	
	_delay_ms(35);
	init_eeprom();
	setup_wx();
	
	
	DDRB |= (1 << 6) | (1 << 7) | (1 << 8);
	
	logged = 0;
	
	OCR1A = GF0 * 2 ;		// Default timeout 3500us

	sei();
	modeftk = 1;
	bits = 0;
	
	PORTB |= (1 << 1);		// clear send port
	
    while(1)
    {
		PORTB &= ~0xE0;

		if (dig_trans_finish == MAX_BITS_LONG)
		{
			INT1_DISABLE;
			no_send = 0;
			check_wx_io();
			if (get_digi_trans())
			{
				logged = 1;
			}
			PORTB |= (1 << 1);		// clear send port
			
			dig_trans_finish = 0;
			
			sei();
			INT1_ENABLE;
			sleep_flag = 1;
		}
		
		if (dig_trans_finish == MAX_BITS_SHORT)
		{
			INT1_DISABLE;
			check_wx_io();
			get_short_trans();
			PORTB |= (1 << 1);		// clear send port
			dig_trans_finish = 0;
			allow_internal_comm = 1;
			bits = 0;
		
			//swl_clear();
			sleep_flag = 1;	
			
			INT1_ENABLE;
		}
		
		if (!logged)
		{
			AL2_OFF;
			AL1_OFF;
		}

		//PORTB ^= (1 << 6); 

	
		rs_pom = 0;
		
		if (sleep_flag)
		{
			PORTB |= 0xE0;
			set_sleep_mode(SLEEP_MODE_STANDBY);
			sleep_enable();
			sleep_mode();

		}
	

		
    }
	
}

void check_wx_io()
{
	ds_alstate = 0;
	//ds_alstate = (IN2 << 3) | (IN1 << 2) | (OUT2 << 1) | (OUT1 << 0);
	
	if (!IN2)
		ds_alstate |= (1 << 3);
	if (!IN1)
		ds_alstate |= (1 << 2);
	
	
	
}

void setup_wx()
{
	
/*
// 	if (ksp == 3)
// 	{
*/
		DDRA |= (1 << 3);		// output
		PORTA |= (1 << 3);
		//PORTA |= (1 << 6) | (1 << 7);
/*
// 	}
// 	else		// for now olny ksp-3
// 	{
// 		
// 	}
*/
	
	PORTD |= (1 << 3);			// int1 pullup as input
	DDRB |= (1 << 1);			// digital_sig_out
	
	DDRC = (1 << 3) | (1 << 2);
	PORTC = 0;
	
	EICRA |= (1 << ISC11);		// falling edge on INT1
	EIMSK |= (1 << INT1);
	
	TIMSK1 |= ( 1 << OCIE1A);
	
	TCCR2B = 0x07;		/// presc 1024
	//TIMSK2 = (1 << TOIE2);
	
	DDRB |= (1 << 7) | (1 << 6) | (1 << 5);
	
}


#define EEPROM_FLAG	0xAE

void init_eeprom()
{
	u8 x = _read(0);
	
	if (x != EEPROM_FLAG)
	{
		nr = 4080;
		write_word(EEP_NR,nr);

		_write(EEP_KSP,1);
		
		leg_date = 12 * 512 + 8 * 32 + 20;
		write_word(EEP_DATE,leg_date);
		
		_write(0,EEPROM_FLAG);
	}
	
	nr		= read_word(EEP_NR);
	modeftk = _read(EEP_KSP);
	
	leg_date = read_word(EEP_DATE);

}



u8 rs_addr;
u16 rs_data;
u16 rs_pom;
u8 rs_crc;

u16 rs_pr = 0;
u16 rs_tm = 0;
u16 rs_rh = 0;

u8 ts_addr;
u8 ts_crc;
u16 ts_data;

u8 loop_cnt = 0;


u16 time_cnt_h = 0;
u16 time_cnt = 0;


u8 ts_repeat = 0;			// repeat current data K-->S x times;

void TS(u8 addr, u16 data, u8 repeat)
{
	ts_addr = addr;
	ts_data = data;
	ts_repeat = repeat;
}

void pulse0()
{
	
	PORTB &= ~(1 << 1);
	WAITUS(F0);
	PORTB |= (1 << 1);
	WAITUS(F0);
	PORTB &= ~(1 << 1);
	WAITUS(F0);
	PORTB |= (1 << 1);
	WAITUS(F0);
	
}

void pulse1()
{
	
	PORTB &= ~(1 << 1);
	WAITUS(F1);
	PORTB |= (1 << 1);
	WAITUS(F1);
	PORTB &= ~(1 << 1);
	WAITUS(F1);
	PORTB |= (1 << 1);
	WAITUS(F1);
	
}